Skip to content

Automate user curation #6590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kitsune/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ def filter_exceptions(event, hint):
DMS_FIX_CURRENT_REVISIONS = config("DMS_FIX_CURRENT_REVISIONS", default=None)
DMS_COHORT_ANALYSIS = config("DMS_COHORT_ANALYSIS", default=None)
DMS_UPDATE_L10N_CONTRIBUTOR_METRICS = config("DMS_UPDATE_L10N_CONTRIBUTOR_METRICS", default=None)
DMS_CLEANUP_EXPIRED_USERS = config("DMS_CLEANUP_EXPIRED_USERS", default=None)

PROD_DETAILS_CACHE_NAME = "product-details"
PROD_DETAILS_STORAGE = config(
Expand Down Expand Up @@ -1331,3 +1332,5 @@ def filter_exceptions(event, hint):

SUMO_BOT_USERNAME = config("SUMO_BOT_USERNAME", default="SumoBot")
SUMO_CONTENT_GROUP = config("SUMO_CONTENT_GROUP", default="Staff Content Team")

USER_EXPIRATION_DAYS = config("USER_EXPIRATION_DAYS", default=1095, cast=int)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe USER_INACTIVITY_DAYS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More descriptive - done!

27 changes: 27 additions & 0 deletions kitsune/users/management/commands/cleanup_expired_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import timedelta

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone

from kitsune.users.utils import delete_user_pipeline


class Command(BaseCommand):
help = "Delete users who haven't logged in for more than settings.USER_EXPIRATION_DAYS days"

def handle(self, *args, **options):
User = get_user_model()
expiration_date = timezone.now() - timedelta(days=settings.USER_EXPIRATION_DAYS)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timezone.now() returns a datetime object. Maybe we should convert this to timezone.now().date()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain further why this should be changed? last_login is a DateTimeField. I guess the precision of the time of day isn't important for this code, but it also doesn't cause a problem as far as I can tell?

Copy link
Collaborator

@akatsoulas akatsoulas Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it does not, it's totally valid. The reason for this nit was the naming of the variable. By converting to date() it just removes the time component which as you said it's not relevant for this kind of functionality. This is just nitpicking so treat it as such


expired_users = User.objects.filter(last_login__lt=expiration_date)
self.stdout.write(f"Found {expired_users.count()} expired users")

for user in expired_users:
delete_user_pipeline(user)
self.stdout.write(f"Deleted user {user.username}")

self.stdout.write(
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users")
Copy link
Preview

Copilot AI Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final success message calls expired_users.count() after users have been deleted, which may result in an incorrect count (likely 0). Consider storing the initial count before processing the deletions and using that stored value in the success message.

Suggested change
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users")
self.style.SUCCESS(f"Successfully processed {expired_users_count} expired users")

Copilot uses AI. Check for mistakes.

)
16 changes: 16 additions & 0 deletions scripts/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,22 @@ def job_cleanup_old_account_events():
call_command("cleanup_old_account_events")


@scheduled_job(
"cron",
month="*",
day="*",
hour="03",
minute="00",
day_of_week=0,
max_instances=1,
coalesce=True,
skip=settings.READ_ONLY,
)
@babis.decorator(ping_after=settings.DMS_CLEANUP_EXPIRED_USERS)
def job_cleanup_expired_users():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a waffle flag/switch to control when this is enabled through admin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

call_command("cleanup_expired_users")


def run():
try:
schedule.start()
Expand Down